package cn.service.impl;

import cn.service.AttributeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.*;
import mapper.AttributeMapper;
import mapper.AttributeValueMapper;
import mapper.CategoryAttributesMapper;
import mapper.CategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class AttributeServiceImpl implements AttributeService {
    @Resource
    CategoryMapper categoryMapper;

    @Resource
    AttributeValueMapper attributeValueMapper;

    @Resource
    CategoryAttributesMapper categoryAttributesMapper;

    @Resource
    AttributeMapper attributeMapper;

    @Override
    public boolean queryByLeftStrAndLevel(String leftStr, Integer level) {
        if (Objects.nonNull(categoryMapper.queryByLeftStrAndLevel(leftStr,level))) {
            //category中存在level0的字段
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean queryByRightStrAndLevel(String rightStr, Integer level) {
        if (Objects.nonNull(categoryMapper.queryByRightStrAndLevel(rightStr,level))) {
            //category中存在level1的字段
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Long queryIdByLeftStr(String leftStr, Integer level) {
        Category category = categoryMapper.queryByLeftStrAndLevel(leftStr, level);
        return category.getCategory_id();
    }

    @Override
    public Result insertLeftStr(Category category) {
        if (categoryMapper.insertSelective(category) > 0) {
            return Result.success(200,"添加成功!");
        } else {
            return Result.fail(500,"添加失败!");
        }
    }

    @Override
    public Result queryByLevel(Integer level,Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Category> categories = categoryMapper.queryByLevel(level);
        for (Category category : categories) {
            String fullName;
            String rightName = category.getName();
            Long parentId = category.getParent_id();
            //根据parent_id找到相应主键的对象获取name做拼接
            Category category1 = categoryMapper.selectByPrimaryKey(parentId);
            String leftStr = category1.getName();
            if ((leftStr.length() != 0)) {
                fullName = leftStr + "-" + rightName;
                category.setName(fullName);
            }
        }
        Page<Category> page = (Page<Category>)categories;
        return Result.success((int)page.getTotal(),categories);
    }

    @Override
    public Result updateByPrimaryKeySelective(Category category) {
        if (categoryMapper.updateByPrimaryKeySelective(category) > 0) {
            return Result.success(200,"编辑成功!");
        } else {
            return Result.fail(500,"编辑失败!");
        }
    }

    @Override
    public Category selectByPrimaryKey(Long categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public Result deleteRow(Long categoryId) {
        if (categoryMapper.deleteByPrimaryKey(categoryId) > 0) {
            return Result.success(200,"删除成功!");
        } else {
            return Result.fail(500,"删除失败!");
        }
    }

    @Override
    public Result deleteAttributeListRow(Integer attribute_id) {
        int i1 = attributeValueMapper.deleteByAttribute_id(attribute_id);
        int i2 = categoryAttributesMapper.deleteByAttribute_id(attribute_id);
        int i3 = attributeMapper.deleteByPrimaryKey(attribute_id);
        if (i1 > 0 && i2 > 0 && i3 > 0) {
            return Result.success(200,"删除成功!");
        } else {
            return Result.fail(500,"删除失败!");
        }
    }

    @Override
    public Result attributeListEdit(DTO dto) {

        Attribute attribute = new Attribute(dto.getAttribute_id(), dto.getAttribute_name());
        attributeMapper.updateByPrimaryKeySelective(attribute);
        attributeValueMapper.deleteByAttribute_id(dto.getAttribute_id());
        List<String> attributeValueList = getAttributeValueList(dto.getIsChooseList());
        for (String s : attributeValueList) {
            AttributeValue attributeValue = new AttributeValue(dto.getAttribute_id(),s);
            attributeValueMapper.insertSelective(attributeValue);
        }
        return Result.success(200,"编辑成功!");
    }

    /**
     * 将输入的isChooseList字符串转为attribute value集合
     * @param isChooseList
     * @return
     */
    public List<String> getAttributeValueList(String isChooseList) {
        String[] array = isChooseList.split(",");
        return Arrays.asList(array);
    }
}
